package com.teng.web.servlet;

import java.io.IOException;
import java.io.Writer;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.teng.ao.QueryUserAo;
import com.teng.dto.JSONResponse;
import com.teng.dto.QueryResult;
import com.teng.dto.UserContext;
import com.teng.exception.OAException;
import com.teng.service.UserCrudService;
import com.teng.service.impl.UserCrudServiceImpl;
import com.teng.util.JSONUtil;

@WebServlet("/api/system/user/list")
public class ListServlet extends HttpServlet{
		@Override
		protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
			req.setCharacterEncoding("utf-8");
			JSONResponse result=new JSONResponse();
			try {
				String key = req.getParameter("key");
				String state = req.getParameter("state");
				String pageNo = req.getParameter("pageNo");
				String pageSize = req.getParameter("pageSize");
				
				QueryUserAo ao=new QueryUserAo();
				
				
				ao.setKey(key);
				ao.setPageNo(pageNo);
				ao.setPageSize(pageSize);
				ao.setState(state);
				
				UserCrudService ucs=new UserCrudServiceImpl();
				HttpSession session=req.getSession();
				UserContext uc=(UserContext)session.getAttribute("LOGIN");
				if(uc==null) {
					throw new OAException("错误");
				}
				QueryResult queryResult=ucs.queryUser(ao,uc.getId());
				
				result.setCode("ok");
				result.setData(queryResult);
			}catch(OAException e){
				result.setCode("001");
				result.setMessage(e.getMessage());
			}catch(Exception e){
				result.setCode("error");
				result.setMessage("系统错误！请稍后再试");
				e.printStackTrace();
			}finally {
				resp.setContentType("application/json");
				resp.setCharacterEncoding("utf-8");
				Writer out=resp.getWriter();
				out.write(JSONUtil.toJsonString(result));
				out.close();
			}
		}
}
